package com.thomas.framework.controller.admin;

import com.thomas.framework.core.annotation.Function;
import com.thomas.framework.core.annotation.FunctionMenu;
import com.thomas.framework.entity.SysDictionary;
import com.thomas.framework.entity.SysDictionaryGroup;
import com.thomas.framework.service.ISysDictionaryService;
import com.thomas.framework.utils.CommonUtil;
import com.thomas.framework.utils.JsonUtil;
import com.thomas.framework.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 字典表管理控制器
 * Created by taohj on 2014/5/23.
 */
@Controller
@RequestMapping("admin/sys/dictionary/dictionary/")
@FunctionMenu("SysDictionaryManager")
public class SysDictionaryController {





    private ISysDictionaryService service;


    @Autowired
    public void setService(ISysDictionaryService service) {
        this.service = service;
    }



    @Function("SysDictionaryCreateView")
    @RequestMapping("createView")
    public  String createView(HttpServletRequest request,@RequestParam("groupId") String groupId){

        request.setAttribute("groupId",groupId);
        return  "admin/sys/dictionary/dictionary/create";
    }



    @Function("SysDictionaryCreate")
    @ResponseBody
    @RequestMapping("create")
    public  String create(
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "value",required = false) String value,
            @RequestParam(value = "groupId",required = false) String groupId,
            @RequestParam(value = "description",required = false) String description
    ){
        PageResult pageResult=new PageResult();
        SysDictionary dictionary=new SysDictionary();
        dictionary.setName(name);
        dictionary.setValue(value);
        dictionary.setGroup(new SysDictionaryGroup(groupId));
        dictionary.setDescription(description);
        pageResult.setSuccess(service.create(dictionary));
        return pageResult.toString();
    }




    @Function("SysDictionaryRemove")
    @ResponseBody
    @RequestMapping("remove")
    public  String remove(@RequestParam("id") String id){
        PageResult pageResult=new PageResult();
        SysDictionary dictionary=new SysDictionary();
        dictionary.setId(id);
        pageResult.setSuccess(service.remove(dictionary));
        return  pageResult.toString();
    }


    @Function("SysDictionaryModifyView")
    @RequestMapping("modifyView")
    public  String  modifyView(HttpServletRequest request,
                               @RequestParam("id") String id){
        SysDictionary dictionary=service.getById(new SysDictionary(id));
        request.setAttribute("dictionary",dictionary);
        return "admin/sys/dictionary/dictionary/modify";
    }

    @Function("SysDictionaryModify")
    @ResponseBody
    @RequestMapping("modify")
    public  String modify(
            @RequestParam(value = "id",required = false) String id,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "value",required = false) String value,
            @RequestParam(value = "description",required = false) String description
    ){
        PageResult pageResult=new PageResult();
        SysDictionary dictionary=new SysDictionary();
        dictionary.setId(id);
        dictionary.setName(name);
        dictionary.setValue(value);
        dictionary.setDescription(description);
        pageResult.setSuccess(service.modify(dictionary));
        return  pageResult.toString();
    }



    @Function("SysDictionaryManagement")
    @RequestMapping("management")
    public  String listView(HttpServletRequest request,@RequestParam("groupId")String groupId){

        request.setAttribute("groupId",groupId);
        return  "admin/sys/dictionary/dictionary/management";
    }


    @Function("SysDictionaryList")
    @ResponseBody
    @RequestMapping("list")
    public String list(
            HttpServletRequest request,
            @RequestParam(value="id",required = false) String id,
            @RequestParam(value="name",required = false) String name,
            @RequestParam(value="groupId") String groupId
    ) {
        PageResult<SysDictionary> pageResult = new PageResult<SysDictionary>();
        pageResult.setT(new SysDictionary());
        pageResult.getT().setId(id);
        pageResult.getT().setName(name);
        pageResult.getT().setGroup(new SysDictionaryGroup(groupId));
        CommonUtil.initPageResult(pageResult, request);
        pageResult=service.getByPage(pageResult);
        return JsonUtil.toString(pageResult);
    }


    @Function("SysDictionaryGetByGroup")
    @ResponseBody
    @RequestMapping("getByGroup")
    public  String getByDictionary(
            @RequestParam("groupName") String groupName
    ){
           SysDictionaryGroup group=new SysDictionaryGroup();
           group.setName(groupName);
           List<SysDictionary> list=service.getListByDictionaryGroup(group);
          SysDictionary d=new SysDictionary();
          d.setValue("");
          d.setName("please Select");
          list.add(0,d);
           return JsonUtil.toString(list);
    }




}
